import $planet_GlobalDC_poof$ from "../../../frame/common/data.global.center";
import { $planet_APP_NAME_poof$ } from "../../../frame/frame.config";
import { $planet_ClientAPI_poof$ } from "../api/client.api";
import $planet_ServiceAPI_poof$ from "../api/service.api";

export default class $planet_Config_poof$ {
	private static $planet_user_id_poof$: number;
	private static $planet_coun_code_poof$: string;
	private static $planet_lang_code_poof$: string;
	private static $planet_count_conf_poof$: Awaited<ReturnType<typeof $planet_ServiceAPI_poof$.$planet_countConf_poof$>>;
	private static $planet_other_conf_poof$: Awaited<ReturnType<typeof $planet_ServiceAPI_poof$.$planet_otherConf_poof$>>;
	private static $planet_device_id_poof$: string;

	/** 用户ID */
	public static get $planet_userID_poof$(){
		return $planet_Config_poof$.$planet_user_id_poof$;
	}
	/** 设备ID */
	public static get $planet_deviceID_poof$(){
		return $planet_Config_poof$.$planet_device_id_poof$;
	}
	/** 用户国家信息 */
	public static get $planet_country_poof$(){
		return $planet_Config_poof$.$planet_coun_code_poof$;
	}
	/** 用户语言信息 */
	public static get $planet_language_poof$(){
		return $planet_Config_poof$.$planet_lang_code_poof$;
	}
	/** 用户国家配置 */
	public static get $planet_countryConfig_poof$(){
		return $planet_Config_poof$.$planet_count_conf_poof$;
	}
	/** 项目其他配置 */
	public static get $planet_otherConfig_poof$(){
		return $planet_Config_poof$.$planet_other_conf_poof$;
	}

	public static async $planet_init_poof$(country: string, language: string){
		$planet_Config_poof$.$planet_user_id_poof$ = abd.$planet_local_poof$.$planet_read_poof$<number>($planet_APP_NAME_poof$+'UID');
		let device_id = await $planet_ClientAPI_poof$.$planet_DeviceID_poof$();
		$planet_Config_poof$.$planet_device_id_poof$ = device_id;

		if(!$planet_Config_poof$.$planet_user_id_poof$){
			let [country, language] = await $planet_ClientAPI_poof$.$planet_Local_poof$().then(res=>['country', 'language'].map(field=>Reflect.get(res, '$planet_'+field+'Code_poof$') as string));
			$planet_Config_poof$.$planet_coun_code_poof$ = country;
			$planet_Config_poof$.$planet_lang_code_poof$ = language;
			await $planet_ServiceAPI_poof$.$planet_userDele_poof$(device_id);
			let user_info = await $planet_ServiceAPI_poof$.$planet_userInit_poof$(device_id, country).then(res=>res);
			[$planet_Config_poof$.$planet_user_id_poof$, $planet_Config_poof$.$planet_coun_code_poof$, $planet_Config_poof$.$planet_lang_code_poof$] =
			[user_info.$planet_userId_poof$, user_info.$planet_country_poof$??country, user_info.$planet_language_poof$??language];

			(<const>[
				['UID', user_info.$planet_userId_poof$],
				['COUNT', user_info.$planet_country_poof$],
				['LANG', user_info.$planet_language_poof$]
			]).forEach(([key, value])=> abd.$planet_local_poof$.$planet_save_poof$($planet_APP_NAME_poof$+key, value) );
		}else{
			[$planet_Config_poof$.$planet_coun_code_poof$, $planet_Config_poof$.$planet_lang_code_poof$] =
			['COUNT', 'LANG'].map(key=>abd.$planet_local_poof$.$planet_read_poof$<string>($planet_APP_NAME_poof$+key));
		}

		[$planet_Config_poof$.$planet_count_conf_poof$, $planet_Config_poof$.$planet_other_conf_poof$] =
		await Promise.all([
			$planet_ServiceAPI_poof$.$planet_countConf_poof$($planet_Config_poof$.$planet_coun_code_poof$),
			$planet_ServiceAPI_poof$.$planet_otherConf_poof$($planet_Config_poof$.$planet_coun_code_poof$),
		]);

		$planet_GlobalDC_poof$.$planet_trigger_poof$.$planet_CONFIG_LOADED_poof$ = true;
	}

	public static $planet_CountryIs_poof$(index: number){
		let code = $planet_country_code_list_poof$[index];
		if(!code) return false;
		return $planet_Config_poof$.$planet_country_poof$===code;
	}

	public static $planet_LanguageIs_poof$(index: number){
		let code = $planet_language_code_list_poof$[index];
		if(!code) return false;
		return $planet_Config_poof$.$planet_language_poof$===code;
	}
}

/** 国家代码列表 */
const $planet_country_code_list_poof$ = [ "QVI=", "QVU=", "QlI=", "Q0E=", "Q0w=", "Q08=", "REU=", "RUc=", "RlI=", "R0I=", "SEs=", "SUQ=", "SU4=", "SlA=", "S1I=", "S1o=", "TU8=", "TVg=", "TVk=", "Tkc=", "Tlo=", "UEg=", "UlU=", "U0E=", "U0c=", "VEg=", "VFI=", "VFc=", "VVM=", "Vk4=", "WkE=" ].map(_=>_.$planet_decodeAtoB_poof$);
/** 语言代码配置 */
const $planet_language_code_list_poof$ = [ "YXI=", "ZGU=", "ZW4=", "ZXM=", "Zmls", "ZnI=", "aGk=", "aWQ=", "amE=", "a2s=", "a28=", "bXM=", "cHQ=", "cnU=", "dGg=", "dHI=", "dmk=", "emg=" ].map(_=>_.$planet_decodeAtoB_poof$);

// Country Code
//"AR": 0
//"AU": 1
//"BR": 2
//"CA": 3
//"CL": 4
//"CO": 5
//"DE": 6
//"EG": 7
//"FR": 8
//"GB": 9
//"HK": 10
//"ID": 11
//"IN": 12
//"JP": 13
//"KR": 14
//"KZ": 15
//"MO": 16
//"MX": 17
//"MY": 18
//"NG": 19
//"NZ": 20
//"PH": 21
//"RU": 22
//"SA": 23
//"SG": 24
//"TH": 25
//"TR": 26
//"TW": 27
//"US": 28
//"VN": 29
//"ZA": 30

// Language Code
//"ar": 0
//"de": 1
//"en": 2
//"es": 3
//"fil": 4
//"fr": 5
//"hi": 6
//"id": 7
//"ja": 8
//"kk": 9
//"ko": 10
//"ms": 11
//"pt": 12
//"ru": 13
//"th": 14
//"tr": 15
//"vi": 16
//"zh": 17